package com.mt4.trade.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.mt4.trade.utils.TradeUtils;

public class LoginAction extends AbstractBaseActionSupport {

	public String index() {

		return SUCCESS;
	}

	public String loginOut() {
		getRequest().getSession().setAttribute("username", "");
		return SUCCESS;
	}

	public String login() {
		String username = this.pm.get("username");
		String password = this.pm.get("password");
		if (username == null || password == null)
			return "error";
		if (username.equals(password)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
			getRequest().getSession().setAttribute("username", username);
			String log = username + "," + sdf.format(new Date()) + ";";
			TradeUtils.writeFileAppend(TradeUtils.LOGIN_LOG, log);
			getRequest().getSession().setAttribute("loginAccount", username);
			tradeService.getUserDataPool().put(username,
					new HashMap<String, Object>());
			return SUCCESS;
		} else {
			getRequest().getSession().setAttribute("username", "");
			return "error";
		}
	}
}
